<?php
class adminAction extends backendAction
{
    public function _initialize() {
        parent::_initialize();
        $this->_mod = D('admin');
		$this->_name = $this->getActionName();
    }
     public function _before_index() {
        $big_menu = array(
            'title' => '添加管理员',
            'iframe' => U($this->_name.'/add'),
            'id' => 'add',
            'width' => '500',
            'height' => '210'
        );
        $this->assign('big_menu', $big_menu);
        $this->list_relation = true;
        //C('zyb_site_ul');
    }

    public function _before_add() {
        $role_list = M('admin_role')->where('status=1')->select();
        $this->assign('role_list', $role_list);
    }
	
    public function _before_insert($data='') {
        if( ($data['password']=='')||(trim($data['password']=='')) ){
            unset($data['password']);
        }else{
            $data['password'] = md5(base64_encode($data['password']));
        }
        return $data;
    }

    public function _before_edit() {
        $this->_before_add();
    }

    public function _before_update($data=''){
        if( ($data['password']=='')||(trim($data['password']=='')) ){
            unset($data['password']);
        }else{
            $data['password'] = md5(base64_encode($data['password']));
        }
        return $data;
    }
    public function edits(){
          if($_POST){
                $password=$this->_post('password','trim');
                $repassword=$this->_post('repassword','trim');
                $email=$this->_post('email','trim');
                if($password!=$repassword)$this->ajaxReturn(0,'两次密码不一致');
                $r=M('admin')->where(array('id'=>$_SESSION['admin']['id']))->save(array(
                'password'=>md5(base64_encode($password)),
                'email'=>$email
                ));
                if($r){
                	$this->ajaxReturn(1);
                }else{
                	$this->ajaxReturn(0,'操作失败,请稍候再试');
                }
          }else{
			  $this->_before_add();
          	    $info=M('admin')->where(array('id'=>$_SESSION['admin']['id']))->find();
          	    $this->assign('info',$info);
                if (IS_AJAX) {
                $response = $this->fetch();
                $this->ajaxReturn(1, '', $response);
            } else {
                $this->display();
            }
          }
    }
    public function ajax_check_name() {
        $name = $this->_get('J_username', 'trim');
        $id = $this->_get('id', 'intval');
        if ($this->_mod->name_exists($name, $id)) {
            echo 0;
        } else {
            echo 1;
        }
    }

    public function syslogs(){
        //默认排序
        $p = $this->_get('p','intval',1);
        $this->assign('p',$p);
        //默认排序
        $this->sort = 'time';
        $this->order = 'desc';
        $map=array();
        $this->_list(M('syslog'), $map);
        $this->display();
    }

}